package com.jiangjiang.eshop.customer.biz;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiangjiang.eshop.customer.constant.ReturnGoodsWorksheetApproveResult;
import com.jiangjiang.eshop.customer.constant.ReturnGoodsWorksheetStatus;
import com.jiangjiang.eshop.customer.convert.ReturnGoodsWorksheetConvert;
import com.jiangjiang.eshop.customer.dto.ReturnGoodsQueryPageDTO;
import com.jiangjiang.eshop.customer.dto.ReturnGoodsWorksheetDTO;
import com.jiangjiang.eshop.customer.service.ReturnGoodsWorksheetService;
import com.jiangjiang.eshop.order.dto.OrderInfoDTO;
import com.jiangjiang.eshop.order.service.OrderService;
import com.jiangjiang.eshop.schedule.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * @Author Jianghaowen
 * @Date 2021/9/9
 * @Description: TODO
 * @Version V1.0
 **/

@Component
public class ReturnGoodsWorksheetBiz {
    @Autowired
    private ReturnGoodsWorksheetService returnGoodsWorksheetService;
    @Autowired
    private ReturnGoodsWorksheetConvert returnGoodsWorksheetConvert;
    @Autowired
    private OrderService orderService;
    @Autowired
    private ScheduleService scheduleService;


    /**
     * 退货工单分页查询
     *
     * @param dto 分页查询条件
     * @return 分页查询结果
     */
    public Page<ReturnGoodsWorksheetDTO> queryForPage(ReturnGoodsQueryPageDTO dto) {
        return returnGoodsWorksheetConvert.convertEntitiesPage2DTO(
                returnGoodsWorksheetService.queryForPage(dto)
        );
    }


    /**
     * 根据id查询退货工单
     *
     * @param id 退货工单id
     * @return 查询结果
     */
    public ReturnGoodsWorksheetDTO queryById(Long id) {
        return returnGoodsWorksheetConvert.convertEntity2DTO(returnGoodsWorksheetService.getById(id));
    }

    /**
     * 退货工单审核
     *
     * @param id            退货工单id
     * @param approveResult 审核结果
     * @return 处理结果
     */
    public Boolean approve(Long id, Integer approveResult) {
        ReturnGoodsWorksheetDTO dto = this.queryById(id);
        Integer status = dto.getStatus();
        if (ReturnGoodsWorksheetApproveResult.PASSED.equals(approveResult)) {
            status = ReturnGoodsWorksheetStatus.WAIT_FOR_SEND_OUT_RETURN_GOODS;
            orderService.informReturnGoodsWorksheetApprovedEvent(dto.getOrderInfoId());
        } else if (ReturnGoodsWorksheetApproveResult.REJECTED.equals(approveResult)) {
            status = ReturnGoodsWorksheetStatus.APPROVE_REJECTED;
            orderService.informReturnGoodsWorksheetRejectedEvent(dto.getOrderInfoId());
        }
        return returnGoodsWorksheetService.updateStatus(id, status);
    }


    /**
     * 确认退货工单已经收到退货商品数据
     *
     * @param id 退货工单id
     * @return 处理结果
     */
    public Boolean confirmReceivedReturnGoods(Long id) {
        // 更新退货工单状态
        ReturnGoodsWorksheetDTO dto = this.queryById(id);
        returnGoodsWorksheetService.updateStatus(id, ReturnGoodsWorksheetStatus.WAIT_FOR_RETURN_GOODS_INPUT);

        // 通知订单中心
        orderService.informReturnGoodsReceivedEvent(dto.getOrderInfoId());

        // 退货商品数据调度入库
        OrderInfoDTO order = orderService.getOrderById(dto.getOrderInfoId());
        return scheduleService.scheduleReturnGoodsInput(order, dto);
    }


}
